package bajo.src;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.component.EditField;
import net.rim.device.api.ui.component.PasswordEditField;
import net.rim.device.api.ui.container.VerticalFieldManager;

/*
 * This class will handle both creating a password text box and non-password
 * text box with a border.
 */
public class TextBoxField extends VerticalFieldManager {

	private EditField txtField;
	private PasswordEditField passField;
	
	private int height;
	private int width;

	public TextBoxField(int height, int width, boolean password) {
		super(Manager.NO_VERTICAL_SCROLL);

		this.height = height;
		this.width = width;

		VerticalFieldManager vfm = new VerticalFieldManager(Manager.VERTICAL_SCROLL);
		
		// Create Password text box.
		if (password) {
			passField = new PasswordEditField();
			txtField = null;
			vfm.add(passField);
		// Create a normal text box.
		} else {
			txtField = new EditField();
			passField = null;
			vfm.add(txtField);
		}

		add(vfm);
	}
	
	public TextBoxField(int height, int width, boolean password, String initTxt) {
		super(Manager.NO_VERTICAL_SCROLL);

		this.height = height;
		this.width = width;

		VerticalFieldManager vfm = new VerticalFieldManager(Manager.VERTICAL_SCROLL);
		
		// Create Password text box.
		if (password) {
			passField = new PasswordEditField(null, initTxt);
			txtField = null;
			vfm.add(passField);
		// Create a normal text box.
		} else {
			txtField = new EditField(null, initTxt);
			passField = null;
			vfm.add(txtField);
		}

		add(vfm);
	}

	public void paint(Graphics g) {
		super.paint(g);
		g.drawRect(0, 0, getWidth(), getHeight());
	}
	
	/*
	 * Adjust the size of the text box with the given width and height.
	 */
	public void sublayout(int width, int height) {
		if (width == 0) {
			this.width = width;
		}
		if (height == 0) {
			this.height = height;
		}
		super.sublayout(this.width, this.height);
		setExtent(this.width,this.height);
	}
	
	/*
	 * Return the input of the text box in String.
	 */
	public String getText() {
		if (txtField == null) {
			return passField.getText();
		}
		return txtField.getText();
	}
	
	/*
	 * Set the input of the text box in String.
	 */
	public void setText(String text) {
		if (txtField == null) {
			passField.setText(text);
		}
		else{
			txtField.setText(text);
		}
	}
	
	
}